package cn.lysj.framework.common.controller;

import cn.hutool.core.util.ImageUtil;
import cn.lysj.framework.base.BaseContent;
import cn.lysj.framework.common.CommonConstant;
import cn.lysj.framework.common.annotation.SystemLog;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zk
 * @description 上传接口
 * @create 2018-08-21 11:24
 **/
@Slf4j
@RestController
@Api(description = "文件上传接口")
@RequestMapping("/upload")
public class UploadController extends BaseContent {


    /**
     * @author ww
     * @date 2018-08-21 16:35
     * @Description
     */
    @PostMapping("/filesUpload")
    @ApiOperation("文件上传")
    public Resp filesUpload(MultipartFile[] files, String module) {
        if (files == null || files.length == 0) {
            return new Resp(Resp.PARAM_ERROR,"请上传文件");
        }
        String dateName = getName();
        String path = CommonConstant.BASE_LOCAL_RESOURCE_IMAGES_DIR + module + "/" + dateName; //默认路径+模块+日期
        String thumbnailPath = path+"/thumbnail/";
        File thumbnailDir = new File(thumbnailPath);
        StringBuilder filenames = new StringBuilder();
        for (int i = 0; i < files.length; i++) {
            if (files[i] == null || files[i].isEmpty()) {
                break;
            }
            String fileName = files[i].getOriginalFilename();// 获取文件名
            String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 获取文件的后缀名
            String descFileName = ParamUtil.uuid() + suffixName;// 设置新的文件名
            File dest = new File(path, descFileName);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                files[i].transferTo(dest);
                if(!thumbnailDir.exists()){
                    thumbnailDir.mkdirs();
                }
                ImageUtil.scale(dest,new File(thumbnailPath+descFileName),120,120, Color.white);
            } catch (IllegalStateException | IOException e) {
                e.printStackTrace();
            }
            filenames.append(descFileName).append("-");
        }
        return new Resp(module+"/"+dateName+"/&"+filenames.substring(0, filenames.length() - 1),"文件上传成功!");
    }

    /**
     * @author ww
     * @date 2018-08-21 16:39qr
     * @Description 日期格式化
     */
    public String getName(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYYMM");
        String dateName = dateFormat.format(new Date());
        return dateName;
    }
}
